package org.linlinjava.litemall.db.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 
import org.linlinjava.litemall.db.dao.LitemallShopMapper;
import org.linlinjava.litemall.db.domain.LitemallShop;
import org.linlinjava.litemall.db.domain.LitemallShop.Column;
import org.linlinjava.litemall.db.domain.LitemallShopExample;
import org.linlinjava.litemall.db.util.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class LitemallShopService {
    private final Column[] idAndName = new Column[]{Column.id, Column.name, Column.types};
    @Resource
    private LitemallShopMapper litemallShopMapper;

    public Map<Integer,LitemallShop> getShoMap(List<Integer> ids) {
        if(CollUtil.isEmpty(ids))return CollUtil.newHashMap();
        List<LitemallShop> list = litemallShopMapper.selectByShopIds(ids);
        return CollectionUtils.convertMap(list,LitemallShop::getId);
    }

    public Page<LitemallShop> selectShopPage(LambdaQueryWrapper<LitemallShop> queryWrapper, Integer page, Integer limit){
        //PageHelper.startPage(page, limit);
        return litemallShopMapper.selectPage(new Page<>(page,limit),queryWrapper);
    }

    public List<LitemallShop> querySelective(Integer shopId, String name, String address, Short status,
                                             LocalDateTime addTimeFrom, LocalDateTime addTimeTo, Integer page,
                                             Integer limit, String sort, String order) {
        LitemallShopExample example = new LitemallShopExample();
        LitemallShopExample.Criteria criteria = example.createCriteria();
        if(null != shopId){
            criteria.andIdEqualTo(shopId);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(status)) {
            criteria.andStatusEqualTo(status);
        }
        if(!StringUtils.isEmpty(address)){
            criteria.andStreetAddressLike("%" + address + "%");
        }
        if (null != addTimeFrom && null != addTimeTo) {
            criteria.andAddTimeBetween(addTimeFrom,addTimeTo);
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        //PageHelper.startPage(page, limit);
        return litemallShopMapper.selectByExampleSelective(example);
    }

    public List<LitemallShop> querySelective(List<Integer> shopIds, String name, String address, Short status,
                                             LocalDateTime addTimeFrom, LocalDateTime addTimeTo, Integer page,
                                             Integer limit, String sort, String order) {
        LitemallShopExample example = new LitemallShopExample();
        LitemallShopExample.Criteria criteria = example.createCriteria();
        if(null != shopIds && shopIds.size() > 0){
            criteria.andIdIn(shopIds);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(status)) {
            criteria.andStatusEqualTo(status);
        }
        if (null != addTimeFrom && null != addTimeTo) {
            criteria.andAddTimeBetween(addTimeFrom,addTimeTo);
        }
        if(!StringUtils.isEmpty(address)){
            criteria.andStreetAddressLike("%" + address + "%");
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        //PageHelper.startPage(page, limit);
        return litemallShopMapper.selectByExampleSelective(example);
    }

    public int updateById(LitemallShop shop) {
        shop.setUpdateTime(LocalDateTime.now());
        //return litemallShopMapper.updateById(shop);
        return litemallShopMapper.updateByPrimaryKeySelective(shop);
    }

    public void deleteById(Integer id) {
        litemallShopMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallShop shop) {
        shop.setAddTime(LocalDateTime.now());
        shop.setUpdateTime(LocalDateTime.now());
        litemallShopMapper.insertSelective(shop);
    }

    public LitemallShop findById(Integer id) {
        return litemallShopMapper.selectByPrimaryKeySelective(id);
    }

    public List<LitemallShop> all() {
        LitemallShopExample example = new LitemallShopExample();
        example.or().andDeletedEqualTo(false);
        return litemallShopMapper.selectByExample(example);
    }

    public List<LitemallShop> getByIds(List<Integer> ids) {
        LitemallShopExample example = new LitemallShopExample();
        example.or().andIdIn(ids).andDeletedEqualTo(false);
        return litemallShopMapper.selectByExampleSelective(example,idAndName);
    }

    public LitemallShop getInfoById(Integer id) {
        LitemallShopExample example = new LitemallShopExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return litemallShopMapper.selectOneByExampleSelective(example,idAndName);
    }

    public int countByName(String name, Integer id) {
        LitemallShopExample example = new LitemallShopExample();
        if(id != null){
            example.or().andNameEqualTo(name.trim()).andIdNotEqualTo(id).andDeletedEqualTo(false);
        }else{
            example.or().andNameEqualTo(name.trim()).andDeletedEqualTo(false);
        }

        return (int)litemallShopMapper.countByExample(example);
    }

    public LitemallShop countByAddress(String streetAddress, Integer id) {
        LitemallShopExample example = new LitemallShopExample();
        if(id != null){
            example.or().andStreetAddressEqualTo(streetAddress.trim()).andIdNotEqualTo(id).andDeletedEqualTo(false);
        }else{
            example.or().andStreetAddressEqualTo(streetAddress.trim()).andDeletedEqualTo(false);
        }
        return litemallShopMapper.selectOneByExample(example);
    }

    public List<LitemallShop> queryAll() {
        LitemallShopExample example = new LitemallShopExample();
        example.or().andDeletedEqualTo(false);
        return litemallShopMapper.selectByExample(example);
    }

    public List<LitemallShop> queryByErpCode(String erpCode) {
        LitemallShopExample example = new LitemallShopExample();
        example.or().andDeletedEqualTo(false).andErpCodeEqualTo(erpCode);
        return litemallShopMapper.selectByExample(example);
    }

    public List<LitemallShop> selectByShopIds(List<Integer> shopIds){
        return litemallShopMapper.selectByShopIds(shopIds);
    }

    public List<LitemallShop> getShopAll() {
        return litemallShopMapper.selectList(Wrappers.lambdaQuery(LitemallShop.class)
                .select(LitemallShop::getId,LitemallShop::getName)
                .eq(LitemallShop::getDeleted,0)
        );
    }

    public long countShop() {
        return litemallShopMapper.selectCount(null);
    }



}
